<!-- 本示例未包含完整css，获取外链css请参考上文，在hello uni-app项目中查看 -->
<template>
	<image class="load" src="/static/loading.gif" v-if="isload"></image>
	<view class="login-dev">
		<button type="primary" class="login-button" @click="userLogin">
			<image src="/static/wechat.svg" style="width: 50rpx;height: 50rpx;padding-top: 20rpx;padding-left: 100rpx;">
			</image>
			<i>微信账号快速登录</i>
		</button>
	</view>

</template>
<script>
	import {
		login
	} from '@/api/api.js'
		import store from '@/store/index.js'; //需要引入store
	export default {
		data() {
			return {
				isload: false
			}
		},
		methods: {
			userLogin() {
				var that = this
				uni.showModal({
					title: "温馨提示",
					content: "授权微信登录后才能正常使用小程序功能",
					success(res) {
						if (res.confirm) {
							uni.login({
								provider: 'weixin',
								success(loginAuth) {
									that.isload = true
									login(loginAuth.code).then(res => {
										console.log(res.data)
										var resp = res.data
										if (resp.code != 0) {
											uni.showToast({
												icon: "none",
												title: "微信登录授权失败"
											})
											that.isload = false
										} else {
											uni.showToast({
												icon: "none",
												title: "微信登录授权成功"
											})
											//登录成功之后获取Token
											uni.setStorage({
												key: 'token',
												data: resp.data.token,
												fail() {
													uni.showToast({
														icon: "none",
														title: "缓存失败"
													})
												}
											})
											if (resp.data.user_type == 0) {
												uni.redirectTo({
													url: '/pages/user/user_identity'
												});
											} else if (resp.data.nick_name == "") {
												uni.redirectTo({
													url: '/pages/user/user_info'
												});
											} else {
												store.state.isAfficheShow =true
												uni.reLaunch({
													url: '/pages/index/index'
												});
											}
										}
									})
								}
							})
						} else {
							uni.showToast({
								icon: "none",
								title: "微信登录授权失败"
							})
							that.isload = false
						}
					},
					fail() {
						uni.showToast({
							icon: "none",
							title: "微信登录授权失败"
						})
						that.isload = false
					}
				})
			}
		}
	}
</script>
<style>
	.login-dev {
		display: flex;
		/* 设置为flex布局 */
		align-items: center;
		/* 垂直居中 */
		justify-content: center;
		/* 水平居中 */
		height: 100vh;
		/* 设置高度为视口高度 */
	}

	.login-button {
		display: flex;
		width: 80%;
	}

	.load {
		display: flex;
		position: absolute;
		margin-top:350rpx;
		z-index:3;
		margin-left:70rpx;
	}
</style>